/**
 * output file path into a file
 */
package cn.netjava.softkid.chapter08;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.LinkedList;

/**
 * @author softkid
 *
 */
public class FilePathInLinkedList {
	LinkedList linkList = new LinkedList();

	/**
	 * add the absolute path and name of each file into LinkedList
	 * @param name: file name
	 */
	public void getAllFileNames(String name,String sufix)
	{
		File f = new File(name);
		//list all files 
		File[] files = f.listFiles();
	
		if(files!=null)
		{
			for(int i=0;i<files.length;i++)
			{
				File temp = files[i];
				
				String fileName=temp.getName();
				
				if(temp.isFile()&&fileName.endsWith(sufix))
				{
					//add the absolute path and name of percent file into LinkedList
					linkList.add(0,temp.getAbsolutePath()+fileName);
				}
				else if(temp.isDirectory())
				{
					//recursion call method getAllFileNames
					getAllFileNames(temp.getAbsolutePath(),sufix);				
				}
			}
		}

	
	}
	/**
	 * output the absolute path and name of each file
	 * @throws FileNotFoundException
	 */
	public void output() throws FileNotFoundException
	{
		for(int i=0;i<linkList.size();i++)
			System.out.println(linkList.get(i));
		
	}
	public void outputIntoFile(String name) throws IOException
	{
		Object[] array=linkList.toArray();
		
		FileWriter fw = new FileWriter(name);
		for(int i=0;i<linkList.size();i++)
		{
			fw.write(i+":"+(String)array[i]+"\r\n");
		}
	}


}
